package com.liuyjy.base.common.desens;


import org.apache.commons.lang3.StringUtils;

/**
 * @Author liuyjy
 * @Date 2022/7/18 20:41
 * @Description: 脱敏验证
 **/
public enum DataMaskingFunc {
    /**
     * 不敏
     */
    NO_MASK((str, maskChar) -> {
        return str;
    }),

    /**
     * 手机号脱敏
     */
    MOBILE((str, maskChar) -> {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }),
    /**
     * 身份证脱敏
     */
    IDCARD((str, maskChar) -> {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", DataMaskingOperation.MASK_CHAR);
    }),
    /**
     * 身份证脱敏
     */
    IDCARD1((str, maskChar) -> {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.replaceAll("(?<=\\w{1})\\w(?=\\w{1})", DataMaskingOperation.MASK_CHAR);
    }),

    /**
     * 中文姓名脱敏
     */
    CHINESE_NAME((str, maskChar) -> {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String serializeValue = "";
        if (str.length() < 3) {
            serializeValue = str.replaceAll(".*(?=[\\u4e00-\\u9fa5])", DataMaskingOperation.MASK_CHAR);
        } else {
            serializeValue = str.replaceAll("(?<=[\\u4e00-\\u9fa5]).*(?=[\\u4e00-\\u9fa5])", DataMaskingOperation.MASK_CHAR);
        }
        return serializeValue;
    }),

    /**
     * 全敏
     */
    ALL_MASK((str, maskChar) -> {
        if (StringUtils.isNotBlank(str)) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                sb.append(StringUtils.isNotBlank(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
            }
            return sb.toString();
        } else {
            return str;
        }
    });

    private final DataMaskingOperation operation;

    private DataMaskingFunc(DataMaskingOperation operation) {
        this.operation = operation;
    }

    public DataMaskingOperation operation() {
        return this.operation;
    }
}
